package com.hmdp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hmdp.dto.Result;
import com.hmdp.dto.UserDTO;
import com.hmdp.entity.Follow;
import com.hmdp.entity.User;
import com.hmdp.mapper.FollowMapper;
import com.hmdp.service.IFollowService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmdp.service.IUserService;
import com.hmdp.utils.RedisConstants;
import com.hmdp.utils.UserHolder;
import net.sf.jsqlparser.expression.LongValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import sun.invoke.empty.Empty;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 虎哥
 * @since 2021-12-22
 */
@Service
public class FollowServiceImpl extends ServiceImpl<FollowMapper, Follow> implements IFollowService {
   @Autowired
   private StringRedisTemplate stringRedisTemplate;
   @Autowired
   private IUserService userService;
    @Override
    public Result follow(Long followUserId, Boolean isFollow) {
        String key= "follows:"+UserHolder.getUser().getId().toString();
        //关注or取关取决于传的isFollow参数
        if(isFollow==true){
            //关注
            Follow follow=new Follow();
           follow.setUserId(UserHolder.getUser().getId());
           follow.setFollowUserId(followUserId);

           follow.setCreateTime(LocalDateTime.now());
            boolean isSuccess = save(follow);
            if(isSuccess){
                //把关注用户的id放入set集合

                stringRedisTemplate.opsForSet().add(key,followUserId.toString());
            }
        }
        else{
         //取关  根据userid与followuserid进行查询
            LambdaQueryWrapper<Follow> queryWrapper=new LambdaQueryWrapper<>();
            queryWrapper.eq(Follow::getUserId,UserHolder.getUser().getId());
            queryWrapper.eq(Follow::getFollowUserId,followUserId);
            //删除数据
            boolean isSuccess = this.remove(queryWrapper);
            if(isSuccess){
                //把关注的用户idc从Redis中移除
                stringRedisTemplate.opsForSet().remove(key,followUserId.toString());
            }
        }
        return Result.ok();
    }

    @Override
    public Result isFollow(Long followUserId) {
        //根据followUserId进行查询
        LambdaQueryWrapper<Follow> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Follow::getUserId,UserHolder.getUser().getId());
        queryWrapper.eq(Follow::getFollowUserId,followUserId);
        int count = this.count(queryWrapper);
        //根据count
        return Result.ok(count>0);
    }

    @Override
    public Result followUserId(Long followUserId) {
        //获取当前登录用户
        UserDTO user = UserHolder.getUser();
        Long id = user.getId();
        //求交集
        String key1="follows:"+id;
        String key2="follows:"+followUserId;
        //两者共同交集
        Set<String> intersect = stringRedisTemplate.opsForSet().intersect(key1, key2);
        //无交集，没有共同关注
        if(intersect==null||intersect.isEmpty()){
            return Result.ok(Collections.emptyList());
        }
        List<UserDTO> users=new ArrayList<>();
        for (String ids : intersect) {
            Long userId = Long.valueOf(ids);
            User user1 = userService.getById(userId);
            UserDTO userDTO = BeanUtil.copyProperties(user1, UserDTO.class);
            users.add(userDTO);

        }
        return Result.ok(users);
    }
}
